<template>
  <div class='container'>
    <el-card>
      <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
        <el-col :span="20">

          <el-form :inline="true" :model="formData" label-width="100px">
            <el-form-item label="目录名称">
              <el-input v-model.trim="formData.text" />
            </el-form-item>
            <el-form-item label="状态">
              <el-select placeholder="请选择" v-model="formData.origin">
                <el-option label="启用" value="1" />
                <el-option label="禁用" value="0" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="away">清除</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">查询</el-button>
            </el-form-item>
          </el-form>

        </el-col>
        <el-col :span="4">
          <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
            <el-col :span="12">
              <el-button
                type="text" plain icon="el-icon-back" size="small"
                @click="fan"
              >返回学科</el-button>
            </el-col>
            <el-col :span="12">
              <el-button type="success" icon="el-icon-edit" size="small" @click="addClick">新增目录</el-button>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-card>
    <!-- 提示数据框 -->
    <el-alert
      :title="`一共有${page.counts}条`"
      type="info"
      show-icon
    />
    <!-- 表格 -->
    <el-table :data="formList" style="width: 100%" v-loading="loading">
      <el-table-column label="序号" type="index" width="100" :index="indexFn" />
      <el-table-column label="所属学科" prop="subjectName" />
      <el-table-column label="目录名称" prop="directoryName" />
      <el-table-column label="创建者" prop="username" />
      <el-table-column label="创建日期" prop="addDate">
        <template #default="{row}">
          {{row.addDate|parseTimeByString}}
        </template>
      </el-table-column>
      <el-table-column label="面试题数量" prop="totals" />
      <el-table-column label="状态" prop="state" :formatter="formatFn" />
      <el-table-column label="操作">
        <template #default="{row}">
          <el-button size="small" type="text" @click="buttonFn(row)">
            {{row.state===1?'启用':'禁用'}}
          </el-button>
          <el-button size="small" type="text" :disabled="row.state===0" @click="huixianFn(row)">修改</el-button>
          <el-button size="small" type="text" :disabled="row.state===0" @click="delclick(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-card
      style="text-align:right
  "
    >
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.page"
        :page-sizes="[1,2,3,4,5,6,7,8,9,10]"
        :page-size="page.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.counts"
      />
    </el-card>
    <!-- 弹框 -->
    <el-dialog :title="addtitle" :visible="isShow" width="30%" @close="closeFN">
      <el-form label-width="80px" :model="couldForm" :rules="rules" ref="foData">
        <el-form-item label="目录名称" prop="one">
          <el-input placeholder="请输入目录名称" style="width:250px" v-model="couldForm.one" />
        </el-form-item>
        <el-form-item label="所属学科" v-if="!$route.query.id">
          <el-select placeholder="请选择" style="width:250px" v-model="page.subjectID">
            <el-option v-for="item in subjectList" :key="item.id" :label="item.subjectName" :value="item.id" />
          </el-select>
        </el-form-item>
        <div style="text-align:center">
          <el-button size="small" @click="closeFN">取消</el-button>
          <el-button size="small" type="primary" @click="addSubmit">确认</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { status } from '../../api/hmmm/constants'
import { listr, add, remove, changeState, update } from '../../api/hmmm/directorys'
import { thdetail } from '../../api/hmmm/subjects'
export default {
  data () {
    return {
      formData: {
        text: '',
        origin: ''
      },
      couldForm: {
        one: '',
        two: ''
      },
      rules: {
        one: [
          { required: true, message: '目录名称不能为空', trigger: ['blur', 'change'] }
        ]

      },
      formList: [],
      valueForm: [],
      subjectList: [],
      // subjectID: 10, // 学科页面传过来的学科的id
      page: {
        page: 1, // 当前页码
        pagesize: 5, // 每页多少条
        counts: 0,
        directoryName: null, // 目录名称
        state: null, // 目录状态
        id: null, // 学科的id
        subjectID: null // 目录列表需要的参数
      },
      isShow: false,
      loading: false,
      tableData: {
        id: '',
        subjectID: '',
        directoryName: ''
      }
    }
  },
  created () {
    this.getDirectory()
    this.getdetail()
  },
  computed: {
    addtitle () {
      return this.page.subjectID ? '编辑目录' : ''
    }
  },
  methods: {

    async getDirectory () {
      this.loading = true
      const { data: { counts, items } } = await listr(this.page)

      this.formList = items
      this.page.counts = counts
      this.loading = false
    },
    handleSizeChange (val) {
      console.log(val)
      this.page.pagesize = val
      this.page.page = 1
      this.getDirectory()
    },
    handleCurrentChange (val) {
      this.page.page = val
      this.getDirectory()
    },
    indexFn (index) {
      return index + 1 + this.page.pagesize * (this.page.page - 1)
    },
    formatFn (row, column, cellValue, index) {
      // const { status } = obj
      const res = status.find(item => item.value === +cellValue)
      return res ? res.label : '未知'
    },
    // 测试
    async getdetail () {
      const { data: { items } } = await thdetail(this.formList.id)
      // console.log(res)
      this.subjectList = items
    },
    // 清除表单
    away () {
      this.formData = {
        text: '',
        origin: ''
      }
    },
    // 查询回到第一页
    async onSubmit () {
      const { data: { items } } = await listr({
        page: 1,
        pagesize: 999999
      })
      console.log(items)
      const res = items.filter(item => item.directoryName === this.formData.text || this.state === +this.formData.origin)
      // console.log(res)
      this.formList = res
      // if (res !== '') {
      //   this.formList = res
      // }
    },
    // 按钮
    async buttonFn (row) {
      if (row.state === 1) {
        row.state = 0
      } else {
        row.state = 1
      }
      await changeState(row)
      this.$message.success('修改状态成功')
      this.getDirectory()
    },
    // 返回学科
    fan () {
      this.$router.push('/subjects/list')
    },
    closeFN () {
      this.isShow = false
      this.couldForm = {
        one: '',
        two: ''
      }
    },
    addClick () {
      this.isShow = true
    },
    addSubmit () {
      // console.log(11)
      this.$refs.foData.validate(async (falg) => {
        if (!falg) return
        this.page.directoryName = this.couldForm.one
        if (this.tableData.id !== '') {
          this.tableData.subjectID = this.page.subjectID
          this.tableData.directoryName = this.couldForm.one
          const res = await update(this.tableData)
          console.log(res)
          this.$message.success('编辑成功')
        } else {
          await add({ ...this.page })
          this.$message.success('添加成功')
        }
        this.closeFN()
        this.getDirectory()
      })
    },
    // 删除
    delclick (row) {
      this.$confirm('你确认要删除嘛', '温馨提示', { type: 'warning' }).then(async () => {
        await remove(row)
        this.$message.success('删除成功')
        if (this.formList.length === 1 && this.page.page > 1) {
          this.page.page--
        }
        this.getDirectory()
      })
    },
    // 回显
    huixianFn (row) {
      this.tableData.id = row.id
      this.isShow = true
      console.log(row)
      this.couldForm.one = row.directoryName
      this.page.subjectID = row.subjectName
    }
  }
}
</script>

<style scoped lang='less'>

</style>
